﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Base/Command
* 类       名: Command
* 功      能:  TD
* 时      间:  2023/6/21 10:14:39
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SiliconSelect.Base
{
    public class Command : ICommand
    {

        private readonly Action<object>? _command;
        private readonly Func<object, bool>? _canExecute;

        public event EventHandler? CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object? parameter)
        {
            if (_canExecute == null)
                return true;
            return _canExecute(parameter ?? throw new ArgumentNullException(nameof(parameter)));
        }

        public void Execute(object? parameter)
        {
            if (parameter != null)
            {
                _command?.Invoke(parameter);
            }
        }


        public Command(Action<object>? command, Func<object, bool>? canExecute = null)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            _canExecute = canExecute;
            _command = command;
        }
    }
}
